package com.xiyuan.action;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xiyuan.dao.ZeouMapper;
import com.xiyuan.entity.Zeou;

/**
 * @author：wujinxing @since：2016-2-27 下午6:25:10
 * @version: 1.0
 * @function:
 */
@Controller
public class ZeouAction {

	@Autowired
	private ZeouMapper zeouMapper;

	// 择偶条件添加
	@RequestMapping(value = "/zeou_add.jsp", method = RequestMethod.GET)
	public String add(@RequestParam(value = "id") String id, ModelMap modelMap) {
		modelMap.put("id", id);
		return "/zeou_add";
	}

	// 择偶条件添加提交
	@RequestMapping(value = "/zeou_add.jsp", method = RequestMethod.POST)
	public String addPost(@ModelAttribute("zeouForm") Zeou zeou, ModelMap modelMap) {
		if (zeou.getHyid() == null) {
			return "500";
		}
		zeouMapper.insertSelective(zeou);
		return "redirect:/huiyuan_list.jsp";

	}

	// 择偶条件修改
	@RequestMapping(value = "/zeou_update.jsp", method = RequestMethod.GET)
	public String update(@RequestParam(value = "id") String id, ModelMap modelMap) {
		Zeou zeou = zeouMapper.selectByHyid(Integer.valueOf(id));
		if (zeou != null)
			modelMap.put("zeou", zeou);
		modelMap.put("id", id);
		return "/zeou_update";
	}

	// 择偶条件添加提交
	@RequestMapping(value = "/zeou_update.jsp", method = RequestMethod.POST)
	public String updatePost(@ModelAttribute("zeouForm") Zeou zeou, ModelMap modelMap) {
		if (zeou.getHyid() == null) {
			return "500";
		}
		Zeou oldZeou = zeouMapper.selectByHyid(zeou.getHyid());
		if (oldZeou == null || oldZeou.getId() == null) {
			 zeouMapper.insertSelective(zeou);
		} else {
			zeou.setId(oldZeou.getId());
			zeouMapper.updateByPrimaryKey(zeou);
		}
		return "redirect:/zeou_detail.jsp?id=" + zeou.getHyid();

	}

	@RequestMapping(value = "/zeou_detail.jsp")
	public String detail(@RequestParam(value = "id") String id, ModelMap modelMap) {
		if (StringUtils.isBlank(id)) {
			return "500";
		}
		Zeou zeou = zeouMapper.selectByHyid(Integer.valueOf(id));
		modelMap.put("zeou", zeou);
		modelMap.put("id", id);
		return "/zeou_detail";
	}

}
